<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Music/classes/PhpFox_ComponentGenreEdit.class.php, $Author:  $ */



/** This component for edit genres
 *   
 * @package module.music
 * @version $Id: PhpFox_ComponentGenreEdit.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 */
class PhpFox_Mod_Music_ComponentGenreEdit extends BaseComponent
{
    /** Processing method
     */
	function process()
	{
	    $oGenreItem = &$this->_oModule->getItem('Genre');
	    /* @var $oGenreItem PhpFox_Mod_Music_ItemGenre */
	    $oMusicService = &$this->_oModule->getService('Music');
	    /* @var $oCustomFieldService PhpFox_Mod_Music_ServiceMusic */
	    $oReq = &App::getRequest();
	    $oUrl = &App::getUrl();
	    $oTpl = &$this->_oModule->getTemplate();

	    $iGenreId = $oReq->getInt('id');
	      
	    // saving genres
	    if ($oReq->getAction() == 'save')
	    {
	        $aGenres = $oReq->getArray('genre');
	        
	        foreach ($aGenres as $mGenreId => $sGenre)
	        {	            
	            if (is_numeric($mGenreId))
	            {
	                $oGenreItem->load($mGenreId);
	                $oGenreItem->aData['genre_name'] = htmlspecialchars($sGenre);
	                $oGenreItem->update(array('genre_name'));
	            }
	        }
	    }
	    
	    // deleting genre
	    if ($oReq->getAction() == 'del_genre')
	    {
	        $iGenreId = $oReq->get('id');
	        if ($iGenreId)
	        {
	            $oGenreItem->oDb->update(App::getT('music_song'),  array('genre_id' => '0'), 'genre_id = "' . $iGenreId . '"');
	            $oGenreItem->oDb->update(App::getT('music_album'), array('genre_id' => '0'), 'genre_id = "' . $iGenreId . '"');
	            $oGenreItem->delete($iGenreId);
	        }
	        
	    }
	    	    
	    // adding new genre
	    if ($oReq->getAction() == 'add')
	    {
	        $sGenre = $oReq->get('genre_new_field');
	        if ($sGenre)
	        {
	            $oGenreItem->aData['genre_id'] = '';
                $oGenreItem->aData['genre_name'] = $sGenre;
                $iNewGenreId = $oGenreItem->insert();
	        }
	    }
	    
	    // going to URL without parameters
        if ($oReq->getAction())
        {
            $oUrl->clearParams('act');
            $oUrl->clearParams('id');
            App::forward($oUrl->getUrl());
        }
	    
	    $aCond = array();

	    $sSort = 'genre_name ';
	    list($aItems, $iCnt) = $oGenreItem->getList($aCond, 0, 0, $sSort);
	    $oTpl->assign(array(
	        'aItems'   => $aItems,
	        'iFieldId' => $iGenreId,
	    ));
	    return $oTpl->fetch('GenreEdit.html');
	}
}
?>